package cn.yzgy.yzgyoa.common.service.impl;

import cn.yzgy.yzgyoa.common.crsp.StatusCode;
import cn.yzgy.yzgyoa.common.dto.SysDictKeyDto;
import cn.yzgy.yzgyoa.common.entity.SysDictKey;
import cn.yzgy.yzgyoa.common.entity.SysDictValue;
import cn.yzgy.yzgyoa.common.exception.SystemException;
import cn.yzgy.yzgyoa.common.mapper.SysDictKeyMapper;
import cn.yzgy.yzgyoa.common.service.ISysDictKeyService;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2021-07-14
 */
@Service
public class SysDictKeyServiceImpl extends ServiceImpl<SysDictKeyMapper, SysDictKey> implements ISysDictKeyService {

    private SysDictValueServiceImpl sysDictValueService;

    public SysDictKeyServiceImpl(SysDictValueServiceImpl sysDictValueService) {
        this.sysDictValueService = sysDictValueService;
    }

    @Override
    public IPage<SysDictKeyDto> getAllDictKey(Page<SysDictKeyDto> dictPage, SysDictKeyDto queryDto) {

        return baseMapper.getAllDictKey(dictPage,queryDto);
    }

    @Override
    public boolean createDictKey(UserInfoDetail userInfoDetail, SysDictKey queryDto) {
        queryDto.setCreateBy(userInfoDetail.getUid()).setCreateTime(LocalDateTime.now());
        return queryDto.insert();
    }

    @Override
    public boolean updateDictKey(UserInfoDetail userInfoDetail, SysDictKey queryDto) {
        boolean b = queryDto.setUpdateBy(userInfoDetail.getUid()).setUpdateTime(LocalDateTime.now()).updateById();
        if(b){
            sysDictValueService.updateDictValueList(userInfoDetail,
                            new SysDictValue()
                                    .setIsSys(queryDto.getIsSys())
                            .setDictCode(queryDto.getDictCode())
            );
        }else{
            throw new SystemException(StatusCode.EXCEPTION,"修改数据字典类型失败！");
        }
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delDictKey(SysDictKeyDto delDto) {
        SysDictKey sysDictKey = new SysDictKey().selectById(delDto.getId());
        if(sysDictKey==null){
            throw new SystemException(StatusCode.FAILED,"当前数据字典未找到，请刷新后重试！");
        }
        if(sysDictKey.getIsSys()){
            throw new SystemException(StatusCode.FAILED,"系统字典，不可删除！");
        }
        //先删除数据字典值表数据
        int delete = sysDictValueService.getBaseMapper().delete(new UpdateWrapper<SysDictValue>().lambda()
                .eq(SysDictValue::getDictCode, sysDictKey.getDictCode()));
        //删除数据字典类型表数据
        delete += baseMapper.delete(new UpdateWrapper<SysDictKey>().lambda()
                .eq(SysDictKey::getDictCode, sysDictKey.getDictCode()));
        return delete > 0;
    }

    /**
     * 查询字典列表
     *
     * @param dictName 字典名
     * @param dictCode 字典码
     * @return 字典列表
     * @since 2021-03-22 14:21:21
     */
    public IPage<SysDictKey> queryAllKey(Page<SysDictKey> page, String dictName, String dictCode) {
        return baseMapper.queryAllKey(page, new LambdaQueryWrapper<SysDictKey>()
                .like(dictCode != null, SysDictKey::getDictCode, dictCode)
                .like(dictName != null, SysDictKey::getDictName, dictName));
    }

}
